1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package sun.security.provider;
27
28 import java.io.*;
29 import java.lang.RuntimePermission;
30 import java.lang.reflect.*;
31 import java.lang.ref.*;
32 import java.net.MalformedURLException;
33 import java.net.URL;
34 import java.net.URI;
35 import java.util.*;
36 import java.util.Enumeration;
37 import java.util.Hashtable;
38 import java.util.List;
39 import java.util.StringTokenizer;
40 import java.util.PropertyPermission;
41 import java.util.ArrayList;
42 import java.util.ListIterator;
43 import java.util.WeakHashMap;
44 import java.text.MessageFormat;
45 import com.sun.security.auth.PrincipalComparator;
46 import java.security.*;
47 import java.security.cert.Certificate;
48 import java.security.cert.X509Certificate;
49 import javax.security.auth.PrivateCredentialPermission;
50 import javax.security.auth.Subject;
51 import javax.security.auth.x500.X500Principal;
52 import java.io.FilePermission;
53 import java.net.SocketPermission;
54 import java.net.NetPermission;
55 import java.util.PropertyPermission;
56 import java.util.concurrent.atomic.AtomicReference;
57
58
59
60
61
62
63
64
65
66
67
68 import sun.misc.JavaSecurityProtectionDomainAccess;
69 import static sun.misc.JavaSecurityProtectionDomainAccess.ProtectionDomainCache;
70 import sun.misc.SharedSecrets;
71 import sun.security.util.Password;
72 import sun.security.util.PolicyUtil;
73 import sun.security.util.PropertyExpander;
74 import sun.security.util.Debug;
75 import sun.security.util.ResourcesMgr;
76 import sun.security.util.SecurityConstants;
77 import sun.net.www.ParseUtil;
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 public class PolicyFile extends java.security.Policy {
284
285 private static final Debug debug = Debug.getInstance("policy");
286
287 private static final String NONE = "NONE";
288 private static final String P11KEYSTORE = "PKCS11";
289
290 private static final String SELF = "${{self}}";
291 private static final String X500PRINCIPAL =
292 "javax.security.auth.x500.X500Principal";
293 private static final String POLICY = "java.security.policy";
294 private static final String SECURITY_MANAGER = "java.security.manager";
295 private static final String POLICY_URL = "policy.url.";
296 private static final String AUTH_POLICY = "java.security.auth.policy";
297 private static final String AUTH_POLICY_URL = "auth.policy.url.";
298
299 private static final int DEFAULT_CACHE_SIZE = 1;
300
301
302 private AtomicReference<PolicyInfo> policyInfo = new AtomicReference<>();
303 private boolean constructed = false;
304
305 private boolean expandProperties = true;
306 private boolean ignoreIdentityScope = true;
307 private boolean allowSystemProperties = true;
308 private boolean notUtf8 = false;
309 private URL url;
310
311
312
313 private static final Class[] PARAMS0 = { };
314 private static final Class[] PARAMS1 = { String.class };
315 private static final Class[] PARAMS2 = { String.class, String.class };
316
317
318
319
320
321 public PolicyFile() {
322 init((URL)null);
323 }
324
325
326
327
328
329 public PolicyFile(URL url) {
330 this.url = url;
331 init(url);
332 }
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435 private void init(URL url) {
436
437
438 String numCacheStr =
439 AccessController.doPrivileged(new PrivilegedAction<String>() {
440 public String run() {
441 expandProperties = "true".equalsIgnoreCase
442 (Security.getProperty("policy.expandProperties"));
443 ignoreIdentityScope = "true".equalsIgnoreCase
444 (Security.getProperty("policy.ignoreIdentityScope"));
445 allowSystemProperties = "true".equalsIgnoreCase
446 (Security.getProperty("policy.allowSystemProperty"));
447 notUtf8 = "false".equalsIgnoreCase
448 (System.getProperty("sun.security.policy.utf8"));
449 return System.getProperty("sun.security.policy.numcaches");
450 }});
451
452 int numCaches;
453 if (numCacheStr != null) {
454 try {
455 numCaches = Integer.parseInt(numCacheStr);
456 } catch (NumberFormatException e) {
457 numCaches = DEFAULT_CACHE_SIZE;
458 }
459 } else {
460 numCaches = DEFAULT_CACHE_SIZE;
461 }
462
463 PolicyInfo newInfo = new PolicyInfo(numCaches);
464 initPolicyFile(newInfo, url);
465 policyInfo.set(newInfo);
466 }
467
468 private void initPolicyFile(final PolicyInfo newInfo, final URL url) {
469
470 if (url != null) {
471
472
473
474
475
476
477 if (debug != null) {
478 debug.println("reading "+url);
479 }
480 AccessController.doPrivileged(new PrivilegedAction<Void>() {
481 public Void run() {
482 if (init(url, newInfo) == false) {
483
484 initStaticPolicy(newInfo);
485 }
486 return null;
487 }
488 });
489
490 } else {
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505 boolean loaded_one = initPolicyFile(POLICY, POLICY_URL, newInfo);
506
507
508 if (!loaded_one) {
509
510 initStaticPolicy(newInfo);
511 }
512
513 initPolicyFile(AUTH_POLICY, AUTH_POLICY_URL, newInfo);
514 }
515 }
516
517 private boolean initPolicyFile(final String propname, final String urlname,
518 final PolicyInfo newInfo) {
519 Boolean loadedPolicy =
520 AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
521 public Boolean run() {
522 boolean loaded_policy = false;
523
524 if (allowSystemProperties) {
525 String extra_policy = System.getProperty(propname);
526 if (extra_policy != null) {
527 boolean overrideAll = false;
528 if (extra_policy.startsWith("=")) {
529 overrideAll = true;
530 extra_policy = extra_policy.substring(1);
531 }
532 try {
533 extra_policy =
534 PropertyExpander.expand(extra_policy);
535 URL policyURL;
536
537 File policyFile = new File(extra_policy);
538 if (policyFile.exists()) {
539 policyURL = ParseUtil.fileToEncodedURL
540 (new File(policyFile.getCanonicalPath()));
541 } else {
542 policyURL = new URL(extra_policy);
543 }
544 if (debug != null)
545 debug.println("reading "+policyURL);
546 if (init(policyURL, newInfo))
547 loaded_policy = true;
548 } catch (Exception e) {
549
550 if (debug != null) {
551 debug.println("caught exception: "+e);
552 }
553 }
554 if (overrideAll) {
555 if (debug != null) {
556 debug.println("overriding other policies!");
557 }
558 return Boolean.valueOf(loaded_policy);
559 }
560 }
561 }
562
563 int n = 1;
564 String policy_uri;
565
566 while ((policy_uri = Security.getProperty(urlname+n)) != null) {
567 try {
568 URL policy_url = null;
569 String expanded_uri = PropertyExpander.expand
570 (policy_uri).replace(File.separatorChar, '/');
571
572 if (policy_uri.startsWith("file:${java.home}/") ||
573 policy_uri.startsWith("file:${user.home}/")) {
574
575
576
577
578
579 policy_url = new File
580 (expanded_uri.substring(5)).toURI().toURL();
581 } else {
582 policy_url = new URI(expanded_uri).toURL();
583 }
584
585 if (debug != null)
586 debug.println("reading "+policy_url);
587 if (init(policy_url, newInfo))
588 loaded_policy = true;
589 } catch (Exception e) {
590 if (debug != null) {
591 debug.println("error reading policy "+e);
592 e.printStackTrace();
593 }
594
595 }
596 n++;
597 }
598 return Boolean.valueOf(loaded_policy);
599 }
600 });
601
602 return loadedPolicy.booleanValue();
603 }
604
605
606
607
608
609
610
611 private boolean init(URL policy, PolicyInfo newInfo) {
612 boolean success = false;
613 PolicyParser pp = new PolicyParser(expandProperties);
614 InputStreamReader isr = null;
615 try {
616
617
618
619
620
621
622 if (notUtf8) {
623 isr = new InputStreamReader
624 (PolicyUtil.getInputStream(policy));
625 } else {
626 isr = new InputStreamReader
627 (PolicyUtil.getInputStream(policy), "UTF-8");
628 }
629
630 pp.read(isr);
631
632 KeyStore keyStore = null;
633 try {
634 keyStore = PolicyUtil.getKeyStore
635 (policy,
636 pp.getKeyStoreUrl(),
637 pp.getKeyStoreType(),
638 pp.getKeyStoreProvider(),
639 pp.getStorePassURL(),
640 debug);
641 } catch (Exception e) {
642
643 if (debug != null) {
644 e.printStackTrace();
645 }
646 }
647
648 Enumeration<PolicyParser.GrantEntry> enum_ = pp.grantElements();
649 while (enum_.hasMoreElements()) {
650 PolicyParser.GrantEntry ge = enum_.nextElement();
651 addGrantEntry(ge, keyStore, newInfo);
652 }
653 } catch (PolicyParser.ParsingException pe) {
654 MessageFormat form = new MessageFormat(ResourcesMgr.getString
655 (POLICY + ".error.parsing.policy.message"));
656 Object[] source = {policy, pe.getLocalizedMessage()};
657 System.err.println(form.format(source));
658 if (debug != null)
659 pe.printStackTrace();
660
661 } catch (Exception e) {
662 if (debug != null) {
663 debug.println("error parsing "+policy);
664 debug.println(e.toString());
665 e.printStackTrace();
666 }
667 } finally {
668 if (isr != null) {
669 try {
670 isr.close();
671 success = true;
672 } catch (IOException e) {
673
674 }
675 } else {
676 success = true;
677 }
678 }
679
680 return success;
681 }
682
683 private void initStaticPolicy(final PolicyInfo newInfo) {
684 AccessController.doPrivileged(new PrivilegedAction<Void>() {
685 public Void run() {
686 PolicyEntry pe = new PolicyEntry(new CodeSource(null,
687 (Certificate[]) null));
688 pe.add(SecurityConstants.LOCAL_LISTEN_PERMISSION);
689 pe.add(new PropertyPermission("java.version",
690 SecurityConstants.PROPERTY_READ_ACTION));
691 pe.add(new PropertyPermission("java.vendor",
692 SecurityConstants.PROPERTY_READ_ACTION));
693 pe.add(new PropertyPermission("java.vendor.url",
694 SecurityConstants.PROPERTY_READ_ACTION));
695 pe.add(new PropertyPermission("java.class.version",
696 SecurityConstants.PROPERTY_READ_ACTION));
697 pe.add(new PropertyPermission("os.name",
698 SecurityConstants.PROPERTY_READ_ACTION));
699 pe.add(new PropertyPermission("os.version",
700 SecurityConstants.PROPERTY_READ_ACTION));
701 pe.add(new PropertyPermission("os.arch",
702 SecurityConstants.PROPERTY_READ_ACTION));
703 pe.add(new PropertyPermission("file.separator",
704 SecurityConstants.PROPERTY_READ_ACTION));
705 pe.add(new PropertyPermission("path.separator",
706 SecurityConstants.PROPERTY_READ_ACTION));
707 pe.add(new PropertyPermission("line.separator",
708 SecurityConstants.PROPERTY_READ_ACTION));
709 pe.add(new PropertyPermission
710 ("java.specification.version",
711 SecurityConstants.PROPERTY_READ_ACTION));
712 pe.add(new PropertyPermission
713 ("java.specification.vendor",
714 SecurityConstants.PROPERTY_READ_ACTION));
715 pe.add(new PropertyPermission
716 ("java.specification.name",
717 SecurityConstants.PROPERTY_READ_ACTION));
718 pe.add(new PropertyPermission
719 ("java.vm.specification.version",
720 SecurityConstants.PROPERTY_READ_ACTION));
721 pe.add(new PropertyPermission
722 ("java.vm.specification.vendor",
723 SecurityConstants.PROPERTY_READ_ACTION));
724 pe.add(new PropertyPermission
725 ("java.vm.specification.name",
726 SecurityConstants.PROPERTY_READ_ACTION));
727 pe.add(new PropertyPermission("java.vm.version",
728 SecurityConstants.PROPERTY_READ_ACTION));
729 pe.add(new PropertyPermission("java.vm.vendor",
730 SecurityConstants.PROPERTY_READ_ACTION));
731 pe.add(new PropertyPermission("java.vm.name",
732 SecurityConstants.PROPERTY_READ_ACTION));
733
734
735 newInfo.policyEntries.add(pe);
736
737
738 String[] extCodebases = PolicyParser.parseExtDirs(
739 PolicyParser.EXTDIRS_EXPANSION, 0);
740 if (extCodebases != null && extCodebases.length > 0) {
741 for (int i = 0; i < extCodebases.length; i++) {
742 try {
743 pe = new PolicyEntry(canonicalizeCodebase(
744 new CodeSource(new URL(extCodebases[i]),
745 (Certificate[]) null), false ));
746 pe.add(SecurityConstants.ALL_PERMISSION);
747
748
749
750 newInfo.policyEntries.add(pe);
751 } catch (Exception e) {
752
753
754 }
755 }
756 }
757 return null;
758 }
759 });
760 }
761
762
763
764
765
766
767 private CodeSource getCodeSource(PolicyParser.GrantEntry ge, KeyStore keyStore,
768 PolicyInfo newInfo) throws java.net.MalformedURLException
769 {
770 Certificate[] certs = null;
771 if (ge.signedBy != null) {
772 certs = getCertificates(keyStore, ge.signedBy, newInfo);
773 if (certs == null) {
774
775
776 if (debug != null) {
777 debug.println(" -- No certs for alias '" +
778 ge.signedBy + "' - ignoring entry");
779 }
780 return null;
781 }
782 }
783
784 URL location;
785
786 if (ge.codeBase != null)
787 location = new URL(ge.codeBase);
788 else
789 location = null;
790
791 return (canonicalizeCodebase(new CodeSource(location, certs),false));
792 }
793
794
795
796
797 private void addGrantEntry(PolicyParser.GrantEntry ge,
798 KeyStore keyStore, PolicyInfo newInfo) {
799
800 if (debug != null) {
801 debug.println("Adding policy entry: ");
802 debug.println(" signedBy " + ge.signedBy);
803 debug.println(" codeBase " + ge.codeBase);
804 if (ge.principals != null && ge.principals.size() > 0) {
805 ListIterator<PolicyParser.PrincipalEntry> li =
806 ge.principals.listIterator();
807 while (li.hasNext()) {
808 PolicyParser.PrincipalEntry pppe = li.next();
809 debug.println(" " + pppe.toString());
810 }
811 }
812 }
813
814 try {
815 CodeSource codesource = getCodeSource(ge, keyStore, newInfo);
816
817 if (codesource == null) return;
818
819
820
821
822
823 if (replacePrincipals(ge.principals, keyStore) == false)
824 return;
825 PolicyEntry entry = new PolicyEntry(codesource, ge.principals);
826 Enumeration<PolicyParser.PermissionEntry> enum_ =
827 ge.permissionElements();
828 while (enum_.hasMoreElements()) {
829 PolicyParser.PermissionEntry pe = enum_.nextElement();
830
831 try {
832
833 expandPermissionName(pe, keyStore);
834
835
836 Permission perm;
837 if (pe.permission.equals
838 ("javax.security.auth.PrivateCredentialPermission") &&
839 pe.name.endsWith(" self")) {
840 pe.name = pe.name.substring(0, pe.name.indexOf("self"))
841 + SELF;
842 }
843
844 if (pe.name != null && pe.name.indexOf(SELF) != -1) {
845
846
847
848
849 Certificate certs[];
850 if (pe.signedBy != null) {
851 certs = getCertificates(keyStore,
852 pe.signedBy,
853 newInfo);
854 } else {
855 certs = null;
856 }
857 perm = new SelfPermission(pe.permission,
858 pe.name,
859 pe.action,
860 certs);
861 } else {
862 perm = getInstance(pe.permission,
863 pe.name,
864 pe.action);
865 }
866 entry.add(perm);
867 if (debug != null) {
868 debug.println(" "+perm);
869 }
870 } catch (ClassNotFoundException cnfe) {
871 Certificate certs[];
872 if (pe.signedBy != null) {
873 certs = getCertificates(keyStore,
874 pe.signedBy,
875 newInfo);
876 } else {
877 certs = null;
878 }
879
880
881
882 if (certs != null || pe.signedBy == null) {
883 Permission perm = new UnresolvedPermission(
884 pe.permission,
885 pe.name,
886 pe.action,
887 certs);
888 entry.add(perm);
889 if (debug != null) {
890 debug.println(" "+perm);
891 }
892 }
893 } catch (java.lang.reflect.InvocationTargetException ite) {
894 MessageFormat form = new MessageFormat
895 (ResourcesMgr.getString
896 (POLICY +
897 ".error.adding.Permission.perm.message"));
898 Object[] source = {pe.permission,
899 ite.getTargetException().toString()};
900 System.err.println(form.format(source));
901 } catch (Exception e) {
902 MessageFormat form = new MessageFormat
903 (ResourcesMgr.getString
904 (POLICY +
905 ".error.adding.Permission.perm.message"));
906 Object[] source = {pe.permission,
907 e.toString()};
908 System.err.println(form.format(source));
909 }
910 }
911
912
913 newInfo.policyEntries.add(entry);
914 } catch (Exception e) {
915 MessageFormat form = new MessageFormat(ResourcesMgr.getString
916 (POLICY
917 + ".error.adding.Entry.message"));
918 Object[] source = {e.toString()};
919 System.err.println(form.format(source));
920 }
921 if (debug != null)
922 debug.println();
923 }
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955 private static final Permission getInstance(String type,
956 String name,
957 String actions)
958 throws ClassNotFoundException,
959 InstantiationException,
960 IllegalAccessException,
961 NoSuchMethodException,
962 InvocationTargetException
963 {
964
965 Class<?> pc = Class.forName(type);
966 Permission answer = getKnownInstance(pc, name, actions);
967 if (answer != null) {
968 return answer;
969 }
970
971 if (name == null && actions == null) {
972 try {
973 Constructor<?> c = pc.getConstructor(PARAMS0);
974 return (Permission) c.newInstance(new Object[] {});
975 } catch (NoSuchMethodException ne) {
976 try {
977 Constructor<?> c = pc.getConstructor(PARAMS1);
978 return (Permission) c.newInstance(
979 new Object[] { name});
980 } catch (NoSuchMethodException ne1 ) {
981 Constructor<?> c = pc.getConstructor(PARAMS2);
982 return (Permission) c.newInstance(
983 new Object[] { name, actions });
984 }
985 }
986 } else {
987 if (name != null && actions == null) {
988 try {
989 Constructor<?> c = pc.getConstructor(PARAMS1);
990 return (Permission) c.newInstance(new Object[] { name});
991 } catch (NoSuchMethodException ne) {
992 Constructor<?> c = pc.getConstructor(PARAMS2);
993 return (Permission) c.newInstance(
994 new Object[] { name, actions });
995 }
996 } else {
997 Constructor<?> c = pc.getConstructor(PARAMS2);
998 return (Permission) c.newInstance(
999 new Object[] { name, actions });
1000 }
1001 }
1002 }
1003
1004
1005
1006
1007
1008
1009 private static final Permission getKnownInstance(Class claz,
1010 String name, String actions) {
1011
1012 if (claz.equals(FilePermission.class)) {
1013 return new FilePermission(name, actions);
1014 } else if (claz.equals(SocketPermission.class)) {
1015 return new SocketPermission(name, actions);
1016 } else if (claz.equals(RuntimePermission.class)) {
1017 return new RuntimePermission(name, actions);
1018 } else if (claz.equals(PropertyPermission.class)) {
1019 return new PropertyPermission(name, actions);
1020 } else if (claz.equals(NetPermission.class)) {
1021 return new NetPermission(name, actions);
1022 } else if (claz.equals(AllPermission.class)) {
1023 return SecurityConstants.ALL_PERMISSION;
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048 } else {
1049 return null;
1050 }
1051 }
1052
1053
1054
1055
1056 private Certificate[] getCertificates
1057 (KeyStore keyStore, String aliases, PolicyInfo newInfo) {
1058
1059 List<Certificate> vcerts = null;
1060
1061 StringTokenizer st = new StringTokenizer(aliases, ",");
1062 int n = 0;
1063
1064 while (st.hasMoreTokens()) {
1065 String alias = st.nextToken().trim();
1066 n++;
1067 Certificate cert = null;
1068
1069 synchronized (newInfo.aliasMapping) {
1070 cert = (Certificate)newInfo.aliasMapping.get(alias);
1071
1072 if (cert == null && keyStore != null) {
1073
1074 try {
1075 cert = keyStore.getCertificate(alias);
1076 } catch (KeyStoreException kse) {
1077
1078
1079 }
1080 if (cert != null) {
1081 newInfo.aliasMapping.put(alias, cert);
1082 newInfo.aliasMapping.put(cert, alias);
1083 }
1084 }
1085 }
1086
1087 if (cert != null) {
1088 if (vcerts == null)
1089 vcerts = new ArrayList<Certificate>();
1090 vcerts.add(cert);
1091 }
1092 }
1093
1094
1095 if (vcerts != null && n == vcerts.size()) {
1096 Certificate[] certs = new Certificate[vcerts.size()];
1097 vcerts.toArray(certs);
1098 return certs;
1099 } else {
1100 return null;
1101 }
1102 }
1103
1104
1105
1106
1107 @Override public void refresh() {
1108 init(url);
1109 }
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124 @Override
1125 public boolean implies(ProtectionDomain pd, Permission p) {
1126 PolicyInfo pi = policyInfo.get();
1127 ProtectionDomainCache pdMap = pi.getPdMapping();
1128
1129 PermissionCollection pc = pdMap.get(pd);
1130
1131 if (pc != null) {
1132 return pc.implies(p);
1133 }
1134
1135 pc = getPermissions(pd);
1136 if (pc == null) {
1137 return false;
1138 }
1139
1140
1141 pdMap.put(pd, pc);
1142 return pc.implies(p);
1143 }
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173 @Override
1174 public PermissionCollection getPermissions(ProtectionDomain domain) {
1175 Permissions perms = new Permissions();
1176
1177 if (domain == null)
1178 return perms;
1179
1180
1181 getPermissions(perms, domain);
1182
1183
1184
1185
1186 PermissionCollection pc = domain.getPermissions();
1187 if (pc != null) {
1188 synchronized (pc) {
1189 Enumeration<Permission> e = pc.elements();
1190 while (e.hasMoreElements()) {
1191 perms.add(e.nextElement());
1192 }
1193 }
1194 }
1195
1196 return perms;
1197 }
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209 @Override
1210 public PermissionCollection getPermissions(CodeSource codesource) {
1211 return getPermissions(new Permissions(), codesource);
1212 }
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224 private PermissionCollection getPermissions(Permissions perms,
1225 ProtectionDomain pd ) {
1226 if (debug != null) {
1227 debug.println("getPermissions:\n\t" + printPD(pd));
1228 }
1229
1230 final CodeSource cs = pd.getCodeSource();
1231 if (cs == null)
1232 return perms;
1233
1234 CodeSource canonCodeSource = AccessController.doPrivileged(
1235 new java.security.PrivilegedAction<CodeSource>(){
1236 public CodeSource run() {
1237 return canonicalizeCodebase(cs, true);
1238 }
1239 });
1240 return getPermissions(perms, canonCodeSource, pd.getPrincipals());
1241 }
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255 private PermissionCollection getPermissions(Permissions perms,
1256 final CodeSource cs) {
1257
1258 CodeSource canonCodeSource = AccessController.doPrivileged(
1259 new java.security.PrivilegedAction<CodeSource>(){
1260 public CodeSource run() {
1261 return canonicalizeCodebase(cs, true);
1262 }
1263 });
1264
1265 return getPermissions(perms, canonCodeSource, null);
1266 }
1267
1268 private Permissions getPermissions(Permissions perms,
1269 final CodeSource cs,
1270 Principal[] principals) {
1271 PolicyInfo pi = policyInfo.get();
1272
1273 for (PolicyEntry entry : pi.policyEntries) {
1274 addPermissions(perms, cs, principals, entry);
1275 }
1276
1277
1278
1279 synchronized (pi.identityPolicyEntries) {
1280 for (PolicyEntry entry : pi.identityPolicyEntries) {
1281 addPermissions(perms, cs, principals, entry);
1282 }
1283 }
1284
1285
1286 if (!ignoreIdentityScope) {
1287 Certificate certs[] = cs.getCertificates();
1288 if (certs != null) {
1289 for (int k=0; k < certs.length; k++) {
1290 Object idMap = pi.aliasMapping.get(certs[k]);
1291 if (idMap == null &&
1292 checkForTrustedIdentity(certs[k], pi)) {
1293
1294
1295
1296
1297 perms.add(SecurityConstants.ALL_PERMISSION);
1298 }
1299 }
1300 }
1301 }
1302 return perms;
1303 }
1304
1305 private void addPermissions(Permissions perms,
1306 final CodeSource cs,
1307 Principal[] principals,
1308 final PolicyEntry entry) {
1309
1310 if (debug != null) {
1311 debug.println("evaluate codesources:\n" +
1312 "\tPolicy CodeSource: " + entry.getCodeSource() + "\n" +
1313 "\tActive CodeSource: " + cs);
1314 }
1315
1316
1317 Boolean imp = AccessController.doPrivileged
1318 (new PrivilegedAction<Boolean>() {
1319 public Boolean run() {
1320 return new Boolean(entry.getCodeSource().implies(cs));
1321 }
1322 });
1323 if (!imp.booleanValue()) {
1324 if (debug != null) {
1325 debug.println("evaluation (codesource) failed");
1326 }
1327
1328
1329 return;
1330 }
1331
1332
1333
1334 List<PolicyParser.PrincipalEntry> entryPs = entry.getPrincipals();
1335 if (debug != null) {
1336 ArrayList<PolicyParser.PrincipalEntry> accPs = new ArrayList<>();
1337 if (principals != null) {
1338 for (int i = 0; i < principals.length; i++) {
1339 accPs.add(new PolicyParser.PrincipalEntry
1340 (principals[i].getClass().getName(),
1341 principals[i].getName()));
1342 }
1343 }
1344 debug.println("evaluate principals:\n" +
1345 "\tPolicy Principals: " + entryPs + "\n" +
1346 "\tActive Principals: " + accPs);
1347 }
1348
1349 if (entryPs == null || entryPs.size() == 0) {
1350
1351
1352
1353
1354 addPerms(perms, principals, entry);
1355 if (debug != null) {
1356 debug.println("evaluation (codesource/principals) passed");
1357 }
1358 return;
1359
1360 } else if (principals == null || principals.length == 0) {
1361
1362
1363
1364
1365 if (debug != null) {
1366 debug.println("evaluation (principals) failed");
1367 }
1368 return;
1369 }
1370
1371
1372
1373
1374
1375 for (int i = 0; i < entryPs.size(); i++) {
1376 PolicyParser.PrincipalEntry pppe = entryPs.get(i);
1377
1378
1379
1380 try {
1381 Class<?> pClass = Class.forName
1382 (pppe.principalClass,
1383 true,
1384 Thread.currentThread().getContextClassLoader());
1385
1386 if (!PrincipalComparator.class.isAssignableFrom(pClass)) {
1387
1388
1389
1390
1391 if (!checkEntryPs(principals, pppe)) {
1392 if (debug != null) {
1393 debug.println("evaluation (principals) failed");
1394 }
1395
1396
1397
1398 return;
1399 }
1400
1401 } else {
1402
1403
1404
1405 Constructor<?> c = pClass.getConstructor(PARAMS1);
1406 PrincipalComparator pc = (PrincipalComparator)c.newInstance
1407 (new Object[] { pppe.principalName });
1408
1409 if (debug != null) {
1410 debug.println("found PrincipalComparator " +
1411 pc.getClass().getName());
1412 }
1413
1414
1415
1416
1417 Set<Principal> pSet = new HashSet<>(principals.length);
1418 for (int j = 0; j < principals.length; j++) {
1419 pSet.add(principals[j]);
1420 }
1421 Subject subject = new Subject(true,
1422 pSet,
1423 Collections.EMPTY_SET,
1424 Collections.EMPTY_SET);
1425
1426 if (!pc.implies(subject)) {
1427 if (debug != null) {
1428 debug.println
1429 ("evaluation (principal comparator) failed");
1430 }
1431
1432
1433
1434 return;
1435 }
1436 }
1437 } catch (Exception e) {
1438
1439
1440
1441 if (debug != null) {
1442 e.printStackTrace();
1443 }
1444
1445 if (!checkEntryPs(principals, pppe)) {
1446 if (debug != null) {
1447 debug.println("evaluation (principals) failed");
1448 }
1449
1450
1451
1452 return;
1453 }
1454 }
1455
1456
1457
1458
1459 }
1460
1461
1462
1463
1464 if (debug != null) {
1465 debug.println("evaluation (codesource/principals) passed");
1466 }
1467 addPerms(perms, principals, entry);
1468 }
1469
1470 private void addPerms(Permissions perms,
1471 Principal[] accPs,
1472 PolicyEntry entry) {
1473 for (int i = 0; i < entry.permissions.size(); i++) {
1474 Permission p = entry.permissions.get(i);
1475 if (debug != null) {
1476 debug.println(" granting " + p);
1477 }
1478
1479 if (p instanceof SelfPermission) {
1480
1481 expandSelf((SelfPermission)p,
1482 entry.getPrincipals(),
1483 accPs,
1484 perms);
1485 } else {
1486 perms.add(p);
1487 }
1488 }
1489 }
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510 private boolean checkEntryPs(Principal[] pList,
1511 PolicyParser.PrincipalEntry pppe) {
1512
1513 for (int i = 0; i < pList.length; i++) {
1514
1515 if (pppe.principalClass.equals
1516 (PolicyParser.PrincipalEntry.WILDCARD_CLASS) ||
1517 pppe.principalClass.equals
1518 (pList[i].getClass().getName())) {
1519
1520 if (pppe.principalName.equals
1521 (PolicyParser.PrincipalEntry.WILDCARD_NAME) ||
1522 pppe.principalName.equals
1523 (pList[i].getName())) {
1524
1525 return true;
1526 }
1527 }
1528 }
1529 return false;
1530 }
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545 private void expandSelf(SelfPermission sp,
1546 List<PolicyParser.PrincipalEntry> entryPs,
1547 Principal[] pdp,
1548 Permissions perms) {
1549
1550 if (entryPs == null || entryPs.size() == 0) {
1551
1552 if (debug != null) {
1553 debug.println("Ignoring permission "
1554 + sp.getSelfType()
1555 + " with target name ("
1556 + sp.getSelfName() + "). "
1557 + "No Principal(s) specified "
1558 + "in the grant clause. "
1559 + "SELF-based target names are "
1560 + "only valid in the context "
1561 + "of a Principal-based grant entry."
1562 );
1563 }
1564 return;
1565 }
1566 int startIndex = 0;
1567 int v;
1568 StringBuilder sb = new StringBuilder();
1569 while ((v = sp.getSelfName().indexOf(SELF, startIndex)) != -1) {
1570
1571
1572 sb.append(sp.getSelfName().substring(startIndex, v));
1573
1574
1575 ListIterator<PolicyParser.PrincipalEntry> pli =
1576 entryPs.listIterator();
1577 while (pli.hasNext()) {
1578 PolicyParser.PrincipalEntry pppe = pli.next();
1579 String[][] principalInfo = getPrincipalInfo(pppe,pdp);
1580 for (int i = 0; i < principalInfo.length; i++) {
1581 if (i != 0) {
1582 sb.append(", ");
1583 }
1584 sb.append(principalInfo[i][0] + " " +
1585 "\"" + principalInfo[i][1] + "\"");
1586 }
1587 if (pli.hasNext()) {
1588 sb.append(", ");
1589 }
1590 }
1591 startIndex = v + SELF.length();
1592 }
1593
1594 sb.append(sp.getSelfName().substring(startIndex));
1595
1596 if (debug != null) {
1597 debug.println(" expanded:\n\t" + sp.getSelfName()
1598 + "\n into:\n\t" + sb.toString());
1599 }
1600 try {
1601
1602 perms.add(getInstance(sp.getSelfType(),
1603 sb.toString(),
1604 sp.getSelfActions()));
1605 } catch (ClassNotFoundException cnfe) {
1606
1607
1608
1609
1610
1611 Class<?> pc = null;
1612 synchronized (perms) {
1613 Enumeration<Permission> e = perms.elements();
1614 while (e.hasMoreElements()) {
1615 Permission pElement = e.nextElement();
1616 if (pElement.getClass().getName().equals(sp.getSelfType())) {
1617 pc = pElement.getClass();
1618 break;
1619 }
1620 }
1621 }
1622 if (pc == null) {
1623
1624 perms.add(new UnresolvedPermission(sp.getSelfType(),
1625 sb.toString(),
1626 sp.getSelfActions(),
1627 sp.getCerts()));
1628 } else {
1629 try {
1630
1631
1632 Constructor<?> c;
1633
1634 if (sp.getSelfActions() == null) {
1635 try {
1636 c = pc.getConstructor(PARAMS1);
1637 perms.add((Permission)c.newInstance
1638 (new Object[] {sb.toString()}));
1639 } catch (NoSuchMethodException ne) {
1640 c = pc.getConstructor(PARAMS2);
1641 perms.add((Permission)c.newInstance
1642 (new Object[] {sb.toString(),
1643 sp.getSelfActions() }));
1644 }
1645 } else {
1646 c = pc.getConstructor(PARAMS2);
1647 perms.add((Permission)c.newInstance
1648 (new Object[] {sb.toString(),
1649 sp.getSelfActions()}));
1650 }
1651 } catch (Exception nme) {
1652 if (debug != null) {
1653 debug.println("self entry expansion " +
1654 " instantiation failed: "
1655 + nme.toString());
1656 }
1657 }
1658 }
1659 } catch (Exception e) {
1660 if (debug != null) {
1661 debug.println(e.toString());
1662 }
1663 }
1664 }
1665
1666
1667
1668
1669
1670
1671
1672 private String[][] getPrincipalInfo
1673 (PolicyParser.PrincipalEntry pe, Principal[] pdp) {
1674
1675
1676
1677
1678
1679
1680 if (!pe.principalClass.equals
1681 (PolicyParser.PrincipalEntry.WILDCARD_CLASS) &&
1682 !pe.principalName.equals
1683 (PolicyParser.PrincipalEntry.WILDCARD_NAME)) {
1684
1685
1686
1687 String[][] info = new String[1][2];
1688 info[0][0] = pe.principalClass;
1689 info[0][1] = pe.principalName;
1690 return info;
1691
1692 } else if (!pe.principalClass.equals
1693 (PolicyParser.PrincipalEntry.WILDCARD_CLASS) &&
1694 pe.principalName.equals
1695 (PolicyParser.PrincipalEntry.WILDCARD_NAME)) {
1696
1697
1698
1699
1700 List<Principal> plist = new ArrayList<>();
1701 for (int i = 0; i < pdp.length; i++) {
1702 if(pe.principalClass.equals(pdp[i].getClass().getName()))
1703 plist.add(pdp[i]);
1704 }
1705 String[][] info = new String[plist.size()][2];
1706 int i = 0;
1707 java.util.Iterator<Principal> pIterator = plist.iterator();
1708 while (pIterator.hasNext()) {
1709 Principal p = pIterator.next();
1710 info[i][0] = p.getClass().getName();
1711 info[i][1] = p.getName();
1712 i++;
1713 }
1714 return info;
1715
1716 } else {
1717
1718
1719
1720
1721 String[][] info = new String[pdp.length][2];
1722
1723 for (int i = 0; i < pdp.length; i++) {
1724 info[i][0] = pdp[i].getClass().getName();
1725 info[i][1] = pdp[i].getName();
1726 }
1727 return info;
1728 }
1729 }
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745 protected Certificate[] getSignerCertificates(CodeSource cs) {
1746 Certificate[] certs = null;
1747 if ((certs = cs.getCertificates()) == null)
1748 return null;
1749 for (int i=0; i<certs.length; i++) {
1750 if (!(certs[i] instanceof X509Certificate))
1751 return cs.getCertificates();
1752 }
1753
1754
1755 int i = 0;
1756 int count = 0;
1757 while (i < certs.length) {
1758 count++;
1759 while (((i+1) < certs.length)
1760 && ((X509Certificate)certs[i]).getIssuerDN().equals(
1761 ((X509Certificate)certs[i+1]).getSubjectDN())) {
1762 i++;
1763 }
1764 i++;
1765 }
1766 if (count == certs.length)
1767
1768 return certs;
1769
1770 ArrayList<Certificate> userCertList = new ArrayList<>();
1771 i = 0;
1772 while (i < certs.length) {
1773 userCertList.add(certs[i]);
1774 while (((i+1) < certs.length)
1775 && ((X509Certificate)certs[i]).getIssuerDN().equals(
1776 ((X509Certificate)certs[i+1]).getSubjectDN())) {
1777 i++;
1778 }
1779 i++;
1780 }
1781 Certificate[] userCerts = new Certificate[userCertList.size()];
1782 userCertList.toArray(userCerts);
1783 return userCerts;
1784 }
1785
1786 private CodeSource canonicalizeCodebase(CodeSource cs,
1787 boolean extractSignerCerts) {
1788
1789 String path = null;
1790
1791 CodeSource canonCs = cs;
1792 URL u = cs.getLocation();
1793 if (u != null) {
1794 if (u.getProtocol().equals("jar")) {
1795
1796 String spec = u.getFile();
1797 int separator = spec.indexOf("!/");
1798 if (separator != -1) {
1799 try {
1800 u = new URL(spec.substring(0, separator));
1801 } catch (MalformedURLException e) {
1802
1803
1804 }
1805 }
1806 }
1807 if (u.getProtocol().equals("file")) {
1808 boolean isLocalFile = false;
1809 String host = u.getHost();
1810 isLocalFile = (host == null || host.equals("") ||
1811 host.equals("~") || host.equalsIgnoreCase("localhost"));
1812
1813 if (isLocalFile) {
1814 path = u.getFile().replace('/', File.separatorChar);
1815 path = ParseUtil.decode(path);
1816 }
1817 }
1818 }
1819
1820 if (path != null) {
1821 try {
1822 URL csUrl = null;
1823 path = canonPath(path);
1824 csUrl = ParseUtil.fileToEncodedURL(new File(path));
1825
1826 if (extractSignerCerts) {
1827 canonCs = new CodeSource(csUrl,
1828 getSignerCertificates(cs));
1829 } else {
1830 canonCs = new CodeSource(csUrl,
1831 cs.getCertificates());
1832 }
1833 } catch (IOException ioe) {
1834
1835
1836 if (extractSignerCerts) {
1837 canonCs = new CodeSource(cs.getLocation(),
1838 getSignerCertificates(cs));
1839 }
1840 }
1841 } else {
1842 if (extractSignerCerts) {
1843 canonCs = new CodeSource(cs.getLocation(),
1844 getSignerCertificates(cs));
1845 }
1846 }
1847 return canonCs;
1848 }
1849
1850
1851
1852 private static String canonPath(String path) throws IOException {
1853 if (path.endsWith("*")) {
1854 path = path.substring(0, path.length()-1) + "-";
1855 path = new File(path).getCanonicalPath();
1856 return path.substring(0, path.length()-1) + "*";
1857 } else {
1858 return new File(path).getCanonicalPath();
1859 }
1860 }
1861
1862 private String printPD(ProtectionDomain pd) {
1863 Principal[] principals = pd.getPrincipals();
1864 String pals = "<no principals>";
1865 if (principals != null && principals.length > 0) {
1866 StringBuilder palBuf = new StringBuilder("(principals ");
1867 for (int i = 0; i < principals.length; i++) {
1868 palBuf.append(principals[i].getClass().getName() +
1869 " \"" + principals[i].getName() +
1870 "\"");
1871 if (i < principals.length-1)
1872 palBuf.append(", ");
1873 else
1874 palBuf.append(")");
1875 }
1876 pals = palBuf.toString();
1877 }
1878 return "PD CodeSource: "
1879 + pd.getCodeSource()
1880 +"\n\t" + "PD ClassLoader: "
1881 + pd.getClassLoader()
1882 +"\n\t" + "PD Principals: "
1883 + pals;
1884 }
1885
1886
1887
1888
1889
1890 private boolean replacePrincipals(
1891 List<PolicyParser.PrincipalEntry> principals, KeyStore keystore) {
1892
1893 if (principals == null || principals.size() == 0 || keystore == null)
1894 return true;
1895
1896 ListIterator<PolicyParser.PrincipalEntry> i = principals.listIterator();
1897 while (i.hasNext()) {
1898 PolicyParser.PrincipalEntry pppe = i.next();
1899 if (pppe.principalClass.equals(PolicyParser.REPLACE_NAME)) {
1900
1901
1902
1903 String name;
1904 if ((name = getDN(pppe.principalName, keystore)) == null) {
1905 return false;
1906 }
1907
1908 if (debug != null) {
1909 debug.println(" Replacing \"" +
1910 pppe.principalName +
1911 "\" with " +
1912 X500PRINCIPAL + "/\"" +
1913 name +
1914 "\"");
1915 }
1916
1917 pppe.principalClass = X500PRINCIPAL;
1918 pppe.principalName = name;
1919 }
1920 }
1921
1922
1923 return true;
1924 }
1925
1926 private void expandPermissionName(PolicyParser.PermissionEntry pe,
1927 KeyStore keystore) throws Exception {
1928
1929 if (pe.name == null || pe.name.indexOf("${{", 0) == -1) {
1930 return;
1931 }
1932
1933 int startIndex = 0;
1934 int b, e;
1935 StringBuilder sb = new StringBuilder();
1936 while ((b = pe.name.indexOf("${{", startIndex)) != -1) {
1937 e = pe.name.indexOf("}}", b);
1938 if (e < 1) {
1939 break;
1940 }
1941 sb.append(pe.name.substring(startIndex, b));
1942
1943
1944 String value = pe.name.substring(b+3, e);
1945
1946
1947 int colonIndex;
1948 String prefix = value;
1949 String suffix;
1950 if ((colonIndex = value.indexOf(":")) != -1) {
1951 prefix = value.substring(0, colonIndex);
1952 }
1953
1954
1955 if (prefix.equalsIgnoreCase("self")) {
1956
1957 sb.append(pe.name.substring(b, e+2));
1958 startIndex = e+2;
1959 continue;
1960 } else if (prefix.equalsIgnoreCase("alias")) {
1961
1962 if (colonIndex == -1) {
1963 MessageFormat form = new MessageFormat
1964 (ResourcesMgr.getString
1965 ("alias.name.not.provided.pe.name."));
1966 Object[] source = {pe.name};
1967 throw new Exception(form.format(source));
1968 }
1969 suffix = value.substring(colonIndex+1);
1970 if ((suffix = getDN(suffix, keystore)) == null) {
1971 MessageFormat form = new MessageFormat
1972 (ResourcesMgr.getString
1973 ("unable.to.perform.substitution.on.alias.suffix"));
1974 Object[] source = {value.substring(colonIndex+1)};
1975 throw new Exception(form.format(source));
1976 }
1977
1978 sb.append(X500PRINCIPAL + " \"" + suffix + "\"");
1979 startIndex = e+2;
1980 } else {
1981 MessageFormat form = new MessageFormat
1982 (ResourcesMgr.getString
1983 ("substitution.value.prefix.unsupported"));
1984 Object[] source = {prefix};
1985 throw new Exception(form.format(source));
1986 }
1987 }
1988
1989
1990 sb.append(pe.name.substring(startIndex));
1991
1992
1993 if (debug != null) {
1994 debug.println(" Permission name expanded from:\n\t" +
1995 pe.name + "\nto\n\t" + sb.toString());
1996 }
1997 pe.name = sb.toString();
1998 }
1999
2000 private String getDN(String alias, KeyStore keystore) {
2001 Certificate cert = null;
2002 try {
2003 cert = keystore.getCertificate(alias);
2004 } catch (Exception e) {
2005 if (debug != null) {
2006 debug.println(" Error retrieving certificate for '" +
2007 alias +
2008 "': " +
2009 e.toString());
2010 }
2011 return null;
2012 }
2013
2014 if (cert == null || !(cert instanceof X509Certificate)) {
2015 if (debug != null) {
2016 debug.println(" -- No certificate for '" +
2017 alias +
2018 "' - ignoring entry");
2019 }
2020 return null;
2021 } else {
2022 X509Certificate x509Cert = (X509Certificate)cert;
2023
2024
2025
2026
2027
2028 X500Principal p = new X500Principal
2029 (x509Cert.getSubjectX500Principal().toString());
2030 return p.getName();
2031 }
2032 }
2033
2034
2035
2036
2037
2038
2039 private boolean checkForTrustedIdentity(final Certificate cert,
2040 PolicyInfo myInfo)
2041 {
2042 return false;
2043 }
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089 private static class PolicyEntry {
2090
2091 private final CodeSource codesource;
2092 final List<Permission> permissions;
2093 private final List<PolicyParser.PrincipalEntry> principals;
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108 PolicyEntry(CodeSource cs, List<PolicyParser.PrincipalEntry> principals)
2109 {
2110 this.codesource = cs;
2111 this.permissions = new ArrayList<Permission>();
2112 this.principals = principals;
2113 }
2114
2115 PolicyEntry(CodeSource cs)
2116 {
2117 this(cs, null);
2118 }
2119
2120 List<PolicyParser.PrincipalEntry> getPrincipals() {
2121 return principals;
2122 }
2123
2124
2125
2126
2127
2128
2129 void add(Permission p) {
2130 permissions.add(p);
2131 }
2132
2133
2134
2135
2136 CodeSource getCodeSource() {
2137 return codesource;
2138 }
2139
2140 @Override public String toString(){
2141 StringBuilder sb = new StringBuilder();
2142 sb.append(ResourcesMgr.getString("LPARAM"));
2143 sb.append(getCodeSource());
2144 sb.append("\n");
2145 for (int j = 0; j < permissions.size(); j++) {
2146 Permission p = permissions.get(j);
2147 sb.append(ResourcesMgr.getString("SPACE"));
2148 sb.append(ResourcesMgr.getString("SPACE"));
2149 sb.append(p);
2150 sb.append(ResourcesMgr.getString("NEWLINE"));
2151 }
2152 sb.append(ResourcesMgr.getString("RPARAM"));
2153 sb.append(ResourcesMgr.getString("NEWLINE"));
2154 return sb.toString();
2155 }
2156 }
2157
2158 private static class SelfPermission extends Permission {
2159
2160 private static final long serialVersionUID = -8315562579967246806L;
2161
2162
2163
2164
2165
2166
2167
2168 private String type;
2169
2170
2171
2172
2173
2174
2175 private String name;
2176
2177
2178
2179
2180
2181
2182 private String actions;
2183
2184
2185
2186
2187
2188
2189 private Certificate certs[];
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204 public SelfPermission(String type, String name, String actions,
2205 Certificate certs[])
2206 {
2207 super(type);
2208 if (type == null) {
2209 throw new NullPointerException
2210 (ResourcesMgr.getString("type.can.t.be.null"));
2211 }
2212 this.type = type;
2213 this.name = name;
2214 this.actions = actions;
2215 if (certs != null) {
2216
2217 for (int i=0; i<certs.length; i++) {
2218 if (!(certs[i] instanceof X509Certificate)) {
2219
2220
2221 this.certs = certs.clone();
2222 break;
2223 }
2224 }
2225
2226 if (this.certs == null) {
2227
2228
2229 int i = 0;
2230 int count = 0;
2231 while (i < certs.length) {
2232 count++;
2233 while (((i+1) < certs.length) &&
2234 ((X509Certificate)certs[i]).getIssuerDN().equals(
2235 ((X509Certificate)certs[i+1]).getSubjectDN())) {
2236 i++;
2237 }
2238 i++;
2239 }
2240 if (count == certs.length) {
2241
2242
2243 this.certs = certs.clone();
2244 }
2245
2246 if (this.certs == null) {
2247
2248 ArrayList<Certificate> signerCerts =
2249 new ArrayList<>();
2250 i = 0;
2251 while (i < certs.length) {
2252 signerCerts.add(certs[i]);
2253 while (((i+1) < certs.length) &&
2254 ((X509Certificate)certs[i]).getIssuerDN().equals(
2255 ((X509Certificate)certs[i+1]).getSubjectDN())) {
2256 i++;
2257 }
2258 i++;
2259 }
2260 this.certs = new Certificate[signerCerts.size()];
2261 signerCerts.toArray(this.certs);
2262 }
2263 }
2264 }
2265 }
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276 @Override public boolean implies(Permission p) {
2277 return false;
2278 }
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293 @Override public boolean equals(Object obj) {
2294 if (obj == this)
2295 return true;
2296
2297 if (! (obj instanceof SelfPermission))
2298 return false;
2299 SelfPermission that = (SelfPermission) obj;
2300
2301 if (!(this.type.equals(that.type) &&
2302 this.name.equals(that.name) &&
2303 this.actions.equals(that.actions)))
2304 return false;
2305
2306 if (this.certs.length != that.certs.length)
2307 return false;
2308
2309 int i,j;
2310 boolean match;
2311
2312 for (i = 0; i < this.certs.length; i++) {
2313 match = false;
2314 for (j = 0; j < that.certs.length; j++) {
2315 if (this.certs[i].equals(that.certs[j])) {
2316 match = true;
2317 break;
2318 }
2319 }
2320 if (!match) return false;
2321 }
2322
2323 for (i = 0; i < that.certs.length; i++) {
2324 match = false;
2325 for (j = 0; j < this.certs.length; j++) {
2326 if (that.certs[i].equals(this.certs[j])) {
2327 match = true;
2328 break;
2329 }
2330 }
2331 if (!match) return false;
2332 }
2333 return true;
2334 }
2335
2336
2337
2338
2339
2340
2341 @Override public int hashCode() {
2342 int hash = type.hashCode();
2343 if (name != null)
2344 hash ^= name.hashCode();
2345 if (actions != null)
2346 hash ^= actions.hashCode();
2347 return hash;
2348 }
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360 @Override public String getActions() {
2361 return "";
2362 }
2363
2364 public String getSelfType() {
2365 return type;
2366 }
2367
2368 public String getSelfName() {
2369 return name;
2370 }
2371
2372 public String getSelfActions() {
2373 return actions;
2374 }
2375
2376 public Certificate[] getCerts() {
2377 return certs;
2378 }
2379
2380
2381
2382
2383
2384
2385
2386
2387 @Override public String toString() {
2388 return "(SelfPermission " + type + " " + name + " " + actions + ")";
2389 }
2390 }
2391
2392
2393
2394
2395 private static class PolicyInfo {
2396 private static final boolean verbose = false;
2397
2398
2399 final List<PolicyEntry> policyEntries;
2400
2401
2402
2403 final List<PolicyEntry> identityPolicyEntries;
2404
2405
2406 final Map aliasMapping;
2407
2408
2409 private final ProtectionDomainCache[] pdMapping;
2410 private java.util.Random random;
2411
2412 PolicyInfo(int numCaches) {
2413 policyEntries = new ArrayList<PolicyEntry>();
2414 identityPolicyEntries =
2415 Collections.synchronizedList(new ArrayList<PolicyEntry>(2));
2416 aliasMapping = Collections.synchronizedMap(new HashMap(11));
2417
2418 pdMapping = new ProtectionDomainCache[numCaches];
2419 JavaSecurityProtectionDomainAccess jspda
2420 = SharedSecrets.getJavaSecurityProtectionDomainAccess();
2421 for (int i = 0; i < numCaches; i++) {
2422 pdMapping[i] = jspda.getProtectionDomainCache();
2423 }
2424 if (numCaches > 1) {
2425 random = new java.util.Random();
2426 }
2427 }
2428 ProtectionDomainCache getPdMapping() {
2429 if (pdMapping.length == 1) {
2430 return pdMapping[0];
2431 } else {
2432 int i = java.lang.Math.abs(random.nextInt() % pdMapping.length);
2433 return pdMapping[i];
2434 }
2435 }
2436 }
2437 }